# Date: Jan 24th, 2021
# Author: geozhang
import json

from rabbitmq.rabbitmq_rpc import MQ_CONFIG, Provider


def provider_process():
    provider = Provider(host=MQ_CONFIG.get("host"),
                        port=MQ_CONFIG.get("port"),
                        virtual_host=MQ_CONFIG.get("virtual_host"),
                        auto_ack=True,
                        durable=True)

    # 生成request消息体
    request_body = []
    for i in range(5):
        request_unit = {}
        n = str(i)
        request_unit["id"] = n
        request_unit["picstr"] = "i am No.%s picture." % n
        request_body.append(request_unit)

    request_body_json = json.dumps(request_body)
    print(' [P] provider send request body: %s' % request_body_json)

    response = provider.call(
        MQ_CONFIG.get("request_exchange01"),
        MQ_CONFIG.get("request_routing_key01"),
        MQ_CONFIG.get("response_queue01"),
        MQ_CONFIG.get("response_exchange01"),
        MQ_CONFIG.get("response_routing_key01"),
        request_body_json)

    return response


if __name__ == '__main__':
    response = provider_process()
    print(" [P] main获取的处理结果：response is %s" % response)
